#include<iostream>
#include<assert.h>
#include<cstdlib>
using std::cout;
using std::endl;
using StackDataUsing = int;
class Stack
{
public:
	Stack(int n);
	~Stack();
	void push(StackDataUsing x);
	StackDataUsing Top();
	void Pop();
	bool Empty();
private:
	StackDataUsing* data;
	int top;
	int capacity;
};

Stack::Stack(int n = 4)
{
	StackDataUsing* newNode = new StackDataUsing[n];
	if (newNode == nullptr)
	{
		perror("new1");
		exit(-1);
	}
	data = newNode;
	capacity = n;
	top = 0;
}

Stack::~Stack()
{
	free(data);
	data = nullptr;
	top = capacity = 0;
}

void Stack::push(StackDataUsing x)
{
	if (capacity == top)
	{
		StackDataUsing* newNode = new StackDataUsing[2 * capacity];
		if (newNode == NULL)
		{
			perror("new2");
			exit(-1);
		}
		data = newNode;
		capacity *= 2;
	}
	data[top++] = x;
}

StackDataUsing Stack::Top()
{
	return data[top - 1];
}
void Stack::Pop()
{
	assert(top > 0);
	top--;
}
bool Stack::Empty()
{
	return top == 0;
}

int main()
{
	Stack st;
	st.push(1);
	st.push(2);
	st.push(3);
	st.push(4);
	while (!st.Empty())
	{
		cout << st.Top() << " ";
		st.Pop();
	} 
	printf("\n");
	system("pause");
	return 0;
}
